package com.lagou.test;

import com.lagou.dao.IUserDao;
import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class IPersistenceTest2 {

    private SqlSession sqlSession;

    @Before
    public void before() throws Exception{
        InputStream resourceAsSteam = Resources.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void testInsert() throws Exception {
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(3);
        user.setUsername("陈俊亘");
        user.setPassword("root");
        user.setBirthday("1993-08-04");
        int result = userDao.insert(user);
        System.out.println(result);
    }

    @Test
    public void testUpdate() throws Exception {
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(3);
        user.setUsername("陈俊亘");
        user.setPassword("root123");
        user.setBirthday("1993-08-04");
        Integer update = userDao.update(user);
        System.out.println(update);

    }

    @Test
    public void testDelete() throws Exception {
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(3);
        user.setUsername("陈俊亘");
        user.setPassword("root123");
        user.setBirthday("1993-08-04");
        Integer delete = userDao.delete(user);
        System.out.println(delete);

    }

    @Test
    public void testSelect() throws Exception {
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        List<User> all = userDao.findAll();
        for (User user : all) {
            System.out.println(user);
        }
    }



}
